var prefix = "/user"
$(function() {
	load();//加载数据
});
/**添加*/
function add(obj){
	layui.use("layer",function(){
		var layer=layui.layer;
		layer.open({
			type : 2,
			title : '添加部门',
			maxmin : true,
			shadeClose : false, // 点击遮罩关闭层
			area : [ '600px', '520px' ],
			content : prefix + '/addDepart?parentId='+obj.id,// 
			end: function(){
//				$('#exampleTable').bootstrapTable('refresh');//刷新table
			}
		});
	})
}
function edit(id) {
	layui.use("layer",function(){
		var layer=layui.layer;
		layer.open({
			type : 2,
			title : '编辑',
			maxmin : true,
			shadeClose : false, // 点击遮罩关闭层
			area : [ '600px', '520px' ],
			content : prefix + '/updateDepart?id=' + id // iframe的url
		});
	})
}
var editObj=null,ptable=null,treeGrid=null,tableId='treeTable',layer=null;
function load(){
	layui.config({
        base: 'plug-in/layuiv2.3.0/extend/'
    }).extend({
        treeGrid:'treeGrid'
    }).use(['jquery','treeGrid','layer'], function(){
    	var $=layui.jquery;
        treeGrid = layui.treeGrid;//很重要
        layer=layui.layer;
        ptable=treeGrid.render({
            id:tableId
            ,elem: '#'+tableId
            ,idField:'id'
            ,url:'/user/departlist'
//            ,url:'/user/treegridlist'
            ,cellMinWidth: 100
            ,treeId:'id'//树形id字段名称
            ,treeUpId:'parentId'//树形父id字段名称
            ,treeShowName:'departName'//以树形式显示的字段
            ,cols: [[
                {field:'departName',width:300, title: '部门名称'}
                ,{field:'orderNum',width:100, title: '排序'}
                ,{field:'delFlag',width:100, title: '状态',templet: function(d){
            		var normal='<span class="badge badge-pill badge-info">正常</span>';
            		var danger='<span class="badge badge-pill badge-danger">失效</span>';
            		return d.delFlag === 0 ? normal : danger;
            	}}
                ,{width:200,title: '操作', align:'center'
                	,templet: function(d){
                		var html='';
                		var editBtn='<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">编辑</a>';
                		var addBtn='<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="add">添加</a>';
                		var delBtn='<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
                		return editBtn+addBtn+delBtn;
                	}
                }
            	/*{field:'name',width:300, title: '部门名称'}
                ,{field:'id',width:100, title: '排序'}
                ,{field:'pId',width:100, title: '状态'}
                ,{width:200,title: '操作', align:'center'
                	,templet: function(d){
                		var html='';
                		var addBtn='<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="add">添加</a>';
                		var delBtn='<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
                		return addBtn+delBtn;
                	}
                }*/
            ]]
            ,page:false
        });

        treeGrid.on('tool('+tableId+')',function (obj) {
            if(obj.event === 'del'){//删除行
            	if(obj.data.children.length ===0){
            		remove(obj.data.id);
            	}else{
            		layer.msg("请先处理子部门");
            	}
            }else if(obj.event==="add"){//添加行
                add(obj.data);
            }else if(obj.event==="edit"){
            	edit(obj.data.id);
            }
        });
    	
    });
}
function reload() {
	treeGrid.reload(tableId,{});
}
function remove(id) {
	layui.use("layer",function(){
		layer.confirm('确定要删除选中的记录？', {
			btn : [ '确定', '取消' ]
		}, function() {
			$.ajax({
				url : prefix + "/deleteDepartByid",
				type : "get",
				data : {
					'id' : id
				},
				success : function(r) {
					if (r.code == 0) {
						layer.msg(r.msg);
					} else {
						layer.msg(r.msg);
					}
					reload();
				}
			});
		})
	})
}

/*function batchRemove() {
	layui.use("layer",function(){
	    var rows = $('#exampleTable').bootstrapTable('getSelections'); // 返回所有选择的行，当没有选择的记录时，返回一个空数组
		if (rows.length == 0) {
			layer.msg("请选择要删除的数据");
			return;
		}
		layer.confirm("确认要删除选中的'" + rows.length + "'条数据吗?", {
			btn : [ '确定', '取消' ]
		// 按钮
		}, function() {
			var ids = "";
			// 遍历所有选择的行数据，取每条数据对应的ID
			$.each(rows, function(i, row) {
				ids = row['id']+","+ids;
			});
			ids=ids.substr(0, ids.length - 1); 
			//var basic = "abc,def,ghi,";  
			//basic = basic.substr(0, basic.length - 1);  
			//console.log(basic);
			$.ajax({
				type : 'POST',
				data : {
					"ids" : ids
				},
				url : prefix + '/deleteUser',
				beforeSend: function(request) {
					request.setRequestHeader("token", getCookie('token'));//添加token
				},
				success : function(r) {
					if (r.code == 0) {
						layer.msg(r.msg);
					} else {
						layer.msg(r.msg);
					}
					$('#exampleTable').bootstrapTable('refresh');//刷新table
				}
			});
		}, function() {});
	})
}*/